namespace Brjnk.Common.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    /// <summary>
    /// </summary>
    public static class ObservableCollectionExtension
    {
        public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)
        {
            foreach (var item in items)
                collection.Add(item);
        }

        public static void RemoveWhere<T>(this ObservableCollection<T> collection, Predicate<T> condition)
        {
            int nextPlace = 0;
            for (int i = 0; i < collection.Count; i++)
            {
                var item = collection[i];
                if (!condition(item))
                {
                    if (nextPlace != i) collection.Move(i, nextPlace);// this[nextPlace] = item;
                    nextPlace++;
                }
            }

            for (int i = collection.Count - 1; i >= nextPlace; i--)
                collection.RemoveAt(i);
        }
    }
}